package jh.utils;

import android.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;


import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;


import org.apache.cordova.MainApplication;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class HttpUtils {

    public static String domain = "http://47.244.180.213:8200";


    public static void POST(String url, Map<String,String> params,final HttpResponse callback){
        url = domain + url;
        Log.i("url",url);

        if (null == params){
            params = new HashMap<>();
        }
        params.put("device","android");
        PostFormBuilder builder = OkHttpUtils.post();
        builder.url(url);
        builder.params(params);
        builder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                callback.callback(null,null,e);
            }

            @Override
            public void onResponse(String response, int id) {

                Log.e("response",response);
                JSONObject responseJSON = null;
                try {
                    responseJSON = new JSONObject(response);
                    if (responseJSON.getString("status").equals("200")){

                        callback.callback(responseJSON,null,null);
                    }else if (responseJSON.getString("status").equals("301")){
                        Toast.makeText(MainApplication.getContent(),responseJSON.getString("message"), Toast.LENGTH_SHORT).show();
                        callback.callback(null,"账号登录信息已过期",null);
                    } else {
                        Toast.makeText(MainApplication.getContent(),responseJSON.getString("message"), Toast.LENGTH_SHORT).show();
                        callback.callback(null,responseJSON.getString("message"),null);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(MainApplication.getContent(),"服务器异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



    public static void GET(String url, Map<String,String> params, final HttpResponse callback){

//        url = domain + url;
//        if (null == params){
//            params = new HashMap<>();
//        }
//        params.put("device","Android");
//
//        GetBuilder builder = OkHttpUtils.get();
//        builder.url(url);
//        builder.url(url);
//        builder.params(params);
//        builder.addHeader("Content-Type", "application/json");
//        builder.build().execute(new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                callback.callback(null,null,e);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//
//
//                Log.d("response",response);
//
//                JSONObject responseJSON = null;
//                try {
//                    responseJSON = new JSONObject(response);
//                    JSONObject jsonObject =  new JSONObject(RSAEncrypt.publicDecrypt(responseJSON.getString("result")));
//                    if (jsonObject.getInt("code") == 200){
//
//                        callback.callback(jsonObject,null,null);
//
//                    }else if (jsonObject.getInt("code") == 104){
//
//                        callback.callback(null,"账号登录信息已过期",null);
//
//                    } else {
//                        callback.callback(null,jsonObject.getString("msg"),null);
//                    }
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });

    }

    public interface HttpResponse{

        void callback(JSONObject object, String msg, Exception e);


    }

}
